package data;


	

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


import domain.File;

public class JDBCFileDAO implements IFileDAO{

    public List<File> selectAllFiles() {
        Connection conn = ConnectionManager.getInstance().checkOut();
	        PreparedStatement stmt = null;
	        List<File> searchResults = new LinkedList<File>();
	        ResultSet result = null;

	        try {

	            String sql = "SELECT * FROM files";
	            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
	            stmt.executeQuery();
	            result = stmt.executeQuery();

	            while (result.next()) {
	                File temp = new File();
	                temp.setId(result.getInt("id"));
	                temp.setName(result.getString("name"));
	                temp.setRoute(result.getString("route"));
	                temp.setDate(result.getDate("date"));
	                searchResults.add(temp);
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            ConnectionManager.getInstance().checkIn(conn);
	            try {
	                if (result != null)
	                    result.close();
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {}
	           
	         
	        }   
	        return searchResults;
	    } 
	  
	    public File select(Connection conn, String FOID) {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        File f = null;
	        String sql = "SELECT * FROM files WHERE (FOID = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, FOID);
	            result = stmt.executeQuery();

	            result.next();
	            f = new File();
	            f.setName(result.getString("name"));
	            f.setRoute(result.getString("price"));
	            f.setDate(result.getDate("date"));
	            f.setId(result.getInt("id"));
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return f;
	    }
	    
	    public String selectFileOID(Connection conn, String id) {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        String oidp = null;
	        String sql = "SELECT * FROM files WHERE (FOID = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, id);
	            result = stmt.executeQuery();

	            result.next();
	            oidp = result.getString("OID");
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oidp;
	    }
	}
	

